using UnityEngine;
using UnityEngine.EventSystems;

public class GUIItemEquipped : MonoBehaviour, IDropHandler, IEventSystemHandler
{
	public ItemCollector CurrentItemCollector;

	public ItemSticker CurrentSticker;

	public string Tag;

	public EquipType Type;

	[HideInInspector]
	public GUIItemCollector guiItem;

	private void Start()
	{
	}

	private void Awake()
	{
		guiItem = GetComponent<GUIItemCollector>();
	}

	public void Clear()
	{
		if (CurrentItemCollector != null)
		{
			CurrentItemCollector.Clear();
		}
		CurrentSticker = null;
	}

	public void Toggle()
	{
		if (UnitZ.playerManager != null && UnitZ.playerManager.PlayingCharacter != null && UnitZ.playerManager.PlayingCharacter.inventory != null)
		{
			UnitZ.playerManager.PlayingCharacter.inventory.ToggleUseEquipped(CurrentItemCollector);
		}
	}

	public void SetupCollector()
	{
		if (!(UnitZ.playerManager.PlayingCharacter == null) && CurrentSticker != null)
		{
			CurrentItemCollector = UnitZ.playerManager.PlayingCharacter.inventory.GetItemCollectorByEquipIndex(CurrentSticker.Index);
			if (guiItem != null)
			{
				guiItem.Item = CurrentItemCollector;
			}
		}
	}

	public void OnDrop(PointerEventData data)
	{
		if (UnitZ.playerManager.PlayingCharacter == null)
		{
			return;
		}
		GUIItemEquipped itemEquipDrop = GetItemEquipDrop(data);
		if (itemEquipDrop != null)
		{
			UnitZ.playerManager.PlayingCharacter.inventory.SwapEquppedSticker(CurrentSticker, itemEquipDrop.CurrentSticker);
			return;
		}
		GUIItemCollector itemCollectorDrop = GetItemCollectorDrop(data);
		if (itemCollectorDrop != null && itemCollectorDrop.Item != null)
		{
			if (itemCollectorDrop.Type == "Inventory" && itemCollectorDrop.Item != null && UnitZ.playerManager.PlayingCharacter.inventory.EquipItemToStickerByCollector(itemCollectorDrop.Item, CurrentSticker))
			{
				UnitZ.playerManager.PlayingCharacter.inventory.ToggleUseEquipped(itemCollectorDrop.Item);
			}
			if (itemCollectorDrop.Type == "Ground" && itemCollectorDrop.Item != null && itemCollectorDrop.Item.Item != null)
			{
				UnitZ.playerManager.PlayingCharacter.Interactive(itemCollectorDrop.Item.Item.gameObject);
				UnitZ.playerManager.PlayingCharacter.inventory.stickerTarget = CurrentSticker;
			}
		}
	}

	public void DropBackToInventory()
	{
		if (CurrentItemCollector != null)
		{
			UnitZ.playerManager.PlayingCharacter.inventory.UnEquipItem(CurrentItemCollector);
		}
	}

	public void DropToGround()
	{
		if (CurrentItemCollector != null)
		{
			UnitZ.playerManager.PlayingCharacter.inventory.DropItemByCollector(CurrentItemCollector, CurrentItemCollector.Num);
		}
	}

	private GUIItemCollector GetItemCollectorDrop(PointerEventData data)
	{
		GameObject pointerDrag = data.pointerDrag;
		if (pointerDrag == null)
		{
			return null;
		}
		if ((bool)pointerDrag.GetComponent<GUIItemCollector>())
		{
			return pointerDrag.GetComponent<GUIItemCollector>();
		}
		return null;
	}

	private GUIItemEquipped GetItemEquipDrop(PointerEventData data)
	{
		GameObject pointerDrag = data.pointerDrag;
		if (pointerDrag == null)
		{
			return null;
		}
		if ((bool)pointerDrag.GetComponent<GUIItemEquipped>())
		{
			return pointerDrag.GetComponent<GUIItemEquipped>();
		}
		return null;
	}
}
